package com.cloudlead.plugin.security;

import com.cloudlead.common.lang.BusinessModule;
import com.cloudlead.common.lang.annotation.Label;
import com.cloudlead.common.worker.action.ActionFactory;
import com.cloudlead.common.worker.annotation.Action;
import com.cloudlead.plugin.security.service.ScPrivilegeService;
import com.cloudlead.plugin.security.service.ScRoleHasPrivilegeService;
import com.cloudlead.plugin.security.service.ScRoleService;
import com.cloudlead.plugin.security.vo.ScPrivilege;
import com.cloudlead.plugin.security.vo.ScRole;
import com.cloudlead.plugin.security.vo.ScRoleHasPrivilege;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ImportPrivilegeInfo {

    ApplicationContext ctx;

    @Before
    public void setUp() throws Exception {
        ctx = new ClassPathXmlApplicationContext(
                "classpath*:META-INF/spring/*.xml");
    }

    @After
    public void tearDown() throws Exception {
        ctx = null;
    }

    @Test
    public void test() {
        try {

            TransactionTemplate transactionTemplate = ctx
                    .getBean(TransactionTemplate.class);
            transactionTemplate.execute(new TransactionCallback<Object>() {

                @Override
                public Object doInTransaction(TransactionStatus status) {
                    ScRoleService roleService = ctx
                            .getBean(ScRoleService.class);
                    ScRole role = new ScRole();
                    role.setName("securityAdmin");
                    role.setDescription("权限管理");
                    role.setAvailable(true);
                    roleService.create(role);

                    ScPrivilegeService moduleService = ctx
                            .getBean(ScPrivilegeService.class);
                    BusinessModules parentBusinessModule = BusinessModules.ApiSecurityModule;
                    ScPrivilege parentModule = new ScPrivilege();
                    parentModule.setName(parentBusinessModule.getName());
                    parentModule.setDescription(parentBusinessModule.getLabel());
                    parentModule.setAvailable(true);
                    moduleService.create(parentModule);
                    BusinessModules[] businessModules = BusinessModules
                            .values();
                    Map<String, ScPrivilege> modules = new HashMap<String, ScPrivilege>();
                    for (int i = 1; i < businessModules.length; i++) {
                        ScPrivilege module = new ScPrivilege();
                        module.setName(businessModules[i].getName());
                        module.setDescription(businessModules[i].getLabel());
                        module.setPid(parentModule.getId());
                        module.setAvailable(true);
                        moduleService.create(module);
                        modules.put(module.getName(), module);
                    }

                    ScPrivilegeService privilegeService = ctx
                            .getBean(ScPrivilegeService.class);
                    ScRoleHasPrivilegeService roleHasPrivilegeService = ctx
                            .getBean(ScRoleHasPrivilegeService.class);
                    Map<String, ActionFactory> map = ctx
                            .getBeansOfType(ActionFactory.class);

                    Set<Entry<String, ActionFactory>> entrySet = map.entrySet();
                    for (Entry<String, ActionFactory> entry : entrySet) {
                        ActionFactory actionFactory = entry.getValue();
                        BusinessModule businessModule = actionFactory
                                .getModule();
                        if (null != businessModule) {
                            String moduleName = businessModule.getName();
                            ScPrivilege scModule = modules.get(moduleName);
                            if (null != scModule) {
                                Class<? extends ActionFactory> actionFactoryCls = actionFactory
                                        .getClass();
                                Method[] methods = actionFactoryCls
                                        .getMethods();
                                for (Method method : methods) {
                                    Action actionAnnotaion = method
                                            .getAnnotation(Action.class);
                                    if (null != actionAnnotaion) {
                                        ScPrivilege privilege = new ScPrivilege();
                                        privilege.setName(businessModule
                                                .getFullName()
                                                + "@"
                                                + method.getName());
                                        Label label = method
                                                .getAnnotation(Label.class);
                                        if (null != label) {
                                            privilege.setDescription(scModule
                                                    .getDescription()
                                                    + label.value());
                                        }
                                        privilege.setPid(scModule.getId());
                                        privilege.setAvailable(true);
                                        privilegeService.create(privilege);
                                        ScRoleHasPrivilege roleHasPrivilege = new ScRoleHasPrivilege();
                                        roleHasPrivilege
                                                .setScPrivilege(privilege);
                                        roleHasPrivilege.setScRole(role);
                                        roleHasPrivilegeService.create(
                                                roleHasPrivilege);
                                    }
                                }
                            }
                        }
                    }
                    return null;
                }

            });

        } catch (BeansException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
